/*
 * ConversionServiceDAO.java
 *
 * Created on April 10, 2007, 12:01 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.jjenkins.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
 * This class contains all the CRUD methods for the conversion service and it allows interaction with the database
 * @author Intex2Week
 */
public class ConversionServiceDAO
{
    /**
     * singleton variable for backupDAO
     */
    private static ConversionServiceDAO instance = null;
    private PreparedStatement readPrice = null;
    
    
    /** Creates a new instance of ConversionServiceDAO */
    private ConversionServiceDAO() {
    }
    
    /**
     * This method implements the singleton pattern. It insures that only
     * on instance of COnversionServiceDAO exists
     * @return instance
     */
    public static synchronized ConversionServiceDAO getInstance() {
        if (instance == null) {
            instance = new ConversionServiceDAO();
        }
        return instance;
    }//end method
    
     /**
     * This method creates a new RevenueSourceBO with a unique QUID, puts the conversion
     * in cache, and then returns it to the calling method.
     * @return conversionServiceBO
     * @param unit_price 
     * @param conversion_type 
     * @throws java.lang.Exception 
     */
    public ConversionService createConvServ(int unit_price, String conversion_type)throws Exception{
        ConversionService p = createConvServ(GUID.generate(),unit_price,conversion_type);
        Cache.getInstance().put(p.getId (),p);
        return p;
    }
    
    /**
     * create print format
     * @param guid 
     * @param unit_price 
     * @param conversion_type 
     * @throws java.lang.Exception 
     * @return 
     */
    public ConversionService createConvServ(String guid, int unit_price, String conversion_type) throws Exception{
        ConversionService p = new ConversionService(guid);
        p.setUnit_price (unit_price);
        p.setConversionType (conversion_type);
        Cache.getInstance().put(p.getId (),p);
        return p;
    }
 
      /**
     * This is the public read statement.  It loads an existing record
     * from the database.
     * @return BackupBO
     * @param type 
     * @throws java.lang.Exception 
     */
    public double readUnitPrice(String type)throws Exception{
        Connection conn = ConnectionPool.getInstance().get();
        return readUnitPrice(type,conn);
    }
    /**
     *  This is a package method that is called by the public read (above) or
     *  by another DAO.  Either way we already have a connection to the database
     *  to use.  The user (controller) code never calls this one directly, since
     *  it can't know about Connection objects or SQLExceptions.
     * @return BackupBO
     * @param type 
     * @param conn connection
     * @throws java.lang.Exception 
     */
    public double readUnitPrice(String type, Connection conn)throws Exception{
        try {
            double price = 0;
            this.readPrice = conn.prepareStatement("select * from conversion_service where conversion_type = ?");
            this.readPrice.setString(1,type);
            ResultSet rs = this.readPrice.executeQuery();
            while(rs.next()){
                price = Double.parseDouble(rs.getString("UNIT_PRICE"));
            }
            ConnectionPool.getInstance().release(conn);
            return price;
        } catch (Exception ex) {
            ex.printStackTrace();
            conn.rollback();
            ConnectionPool.getInstance().release(conn);
            throw ex;
        } 
    }
    
  

    
}
